package com.hjc.springbootmovies.controller;

import com.hjc.springbootmovies.entity.ModelSale;
import com.hjc.springbootmovies.service.Impl.HallServiceImpl;
import com.hjc.springbootmovies.service.Impl.SaleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: HJC
 * Date: 2019/12/4
 * Time: 14:34
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/sale")
public class SaleController {
    @Autowired
    private SaleServiceImpl saleService;
    @Autowired
    private HallServiceImpl hallService;
    @GetMapping("saleControllerAll")
    public String salAll(){
        return "html/sale/all";
    }

    @GetMapping("saleControllerBroadTime")
    public String salBroadTime(){
        return "html/sale/broadTime";
    }

    @GetMapping("saleControllerFiml")
    public String salFiml(String selectTime , HttpServletRequest request){
        String arr[] = selectTime.split("z");
        Arrays.stream(arr).forEach(System.out::println);
        request.getSession().setAttribute("begintime",arr[0]);
        request.getSession().setAttribute("endtime",arr[1]);
        return "html/sale/fiml";
    }

    @GetMapping("saleControllerHall")
    public String salHall(int fid , HttpServletRequest request){
        request.getSession().setAttribute("fid",fid);
        return "html/sale/hall";
    }

    @GetMapping("saleControllerSeat")
    public String salSeat(int tid , Model model , HttpServletRequest request){
        int count = hallService.queryById(tid).getTcount();
        request.getSession().setAttribute("tid",tid);
        model.addAttribute("count",count);
        model.addAttribute("tidsa" , tid);
        return "html/sale/seat";
    }

    /*Transactional 事务*/
    @RequestMapping( value = "addSale" , method = RequestMethod.POST)
    @Transactional
    public String addSale(int seat , HttpServletRequest request){
        String begintime = (String)request.getSession().getAttribute("begintime");
        String endtime = (String)request.getSession().getAttribute("endtime");
        int fid = (int)request.getSession().getAttribute("fid");
        int tid = (int)request.getSession().getAttribute("tid");
        System.out.println(fid + " " + tid + " " + seat);
        int count = saleService.addSale(fid,tid,seat);
        return "html/sale/all";
    }

    @RequestMapping( value = "deleteById" , method = RequestMethod.GET)
    public String deleteById(int mid){
        int count = saleService.deleteById(mid);
        return "html/sale/all";
    }
    /*
    @RequestMapping( value = "updateSale" , method = RequestMethod.POST)
    public String updateSale(ModelSale modelSale){
        int count = saleService.updateSale(modelSale);
        return "html/sale/all";
    }
     */
}
